package dataaccess;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import domain.User;

public class UsersDB {
	
	Connection con;                                                                                                           
	Statement st;                                                                                                            
	PreparedStatement ps;
	private InitialContext envCtx;
	private DataSource ds;
	public UsersDB() {                                                                                      
		try {

			envCtx = new InitialContext();
			Context initContext = (Context) envCtx.lookup("java:comp/env");
			// Look up our data source
			ds = (DataSource) initContext.lookup("jdbc/mikes");
			// Obtain our environment naming context

		} catch (NamingException e) {
			e.printStackTrace();
		}
		try {
			con = ds.getConnection();
			System.out.println("connected to database");
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("can't connect to the database");
		}
		try {
			st = con.createStatement();
		} catch (SQLException e) {

			e.printStackTrace();
		}
	}
	
	
	public boolean checkLogin(User user){
	
		try {
			if(con.isClosed()||con==null){
				con = ds.getConnection();
			}
			st=con.createStatement();
			ResultSet rs;
			rs= st.executeQuery("SELECT * FROM users WHERE userName='"+user.getUserName()+"'and passWord='"+user.getPassword()+"'");
			System.out.println("retrieved "+rs.toString()+ " from the database");
			if(rs!=null){
				
				System.out.println("result set not null checking the user info");
				while (rs.next()) {
					if(user.getUserName().equals(rs.getString("userName"))&&user.getPassword().equals(rs.getString("passWord"))){
							return true;
					}
				}
			}	
	
		} catch (SQLException e) {

			e.printStackTrace();
		} finally {

			try {
				st.close();
				con.close();
				System.out.println("Connection closed");

			} catch (SQLException e) {
				
				e.printStackTrace();
			}

		}
		System.out.println("no resultset for the query");
		return false;

	}
	
	public User getUserInfoByUserName(String username) {
		User user = new User();
		try {
			if (con == null) {
				con = ds.getConnection();
			}
			st = con.createStatement();
			ResultSet rs = st
					.executeQuery("SELECT * FROM users WHERE userName='"
							+ username + "'");

			user.setUserName(rs.getString(1));
			user.setPassword(rs.getString(2));
			user.setAccessLevel(rs.getString(3));
		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			try {
				st.close();
				con.close();
				System.out.println("Connection closed");
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return user;
	}
}